﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using DTO.Models;
using System.Text.RegularExpressions;

namespace CIT.Controllers
{
    public class KhaosatController : Controller
    {
        private const string TempDataKey = "CaptchaData";
        private const string SessionKey = "CaptchaSolution";
        //
        // GET: /Khaosat/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult KhaoSatTop()
        {
            DataRepository dr = new DataRepository();
            SURVEY survey = dr.SurveyProvider.getSurveyOut();
            return View(survey);
        }

        public ActionResult KetQuaKhaoSat()
        {
            DataRepository dr = new DataRepository();
            SURVEY obj = dr.SurveyProvider.getSurveyOut();
            if (obj != null)
            {
                int len = 0;
                if (obj.Answer1.Length != 0)
                {
                    len += 1;
                }
                if (obj.Answer2.Length != 0)
                {
                    len += 1;
                }
                if (obj.Answer3.Length != 0)
                {
                    len += 1;
                }
                if (obj.Answer4.Length != 0)
                {
                    len += 1;
                }
                if (obj.Answer5.Length != 0)
                {
                    len += 1;
                }
                if (obj.Answer6.Length != 0)
                {
                    len += 1;
                }
                ViewData["len"] = len.ToString();
                return View(obj);
            }
            return View("Error");
        }

        [HttpPost]
        public ActionResult KhaoSatTop(string id)
        {
            string ip = Request.UserHostAddress;
            int answer = Convert.ToInt32(id);

            DataRepository dr = new DataRepository();
            SURVEY obj = dr.SurveyProvider.getSurveyOut();

            string flag = "0";

            //       //email neu rong thi ko cho vote
            //       if (String.IsNullOrEmpty(email))
            //       {
            //           return Content("-1");
            //       }
            //       else
            //       {
            //           string MatchEmailPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
            //+ @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]? [0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
            //+ @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]? [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
            //+ @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

            //           bool isValidEmail = false;
            //           isValidEmail = Regex.IsMatch(email, MatchEmailPattern);
            //           if (isValidEmail == false)
            //               return Content("-1");
            //       }
            if (obj != null)
            {
                string cookie = "";
                if (Request.Cookies["vote"] != null)
                {
                    cookie = Request.Cookies["vote"].Value;
                    if (dr.VoteProvider.checkHaveVote(obj.ID, cookie))
                    {
                        flag = "2";
                    }
                }

                if (Request.Cookies["vote"] == null || flag.CompareTo("0") == 0)
                {
                    switch (answer)
                    {
                        case 1:
                            obj.Total1 += 1;
                            break;
                        case 2:
                            obj.Total2 += 1;
                            break;
                        case 3:
                            obj.Total3 += 1;
                            break;
                        case 4:
                            obj.Total4 += 1;
                            break;
                        case 5:
                            obj.Total5 += 1;
                            break;
                        case 6:
                            obj.Total6 += 1;
                            break;
                    }

                    VOTE vote = new VOTE();
                    vote.SurveyId = obj.ID;
                    vote.Ip = ip + "-" + obj.ID.ToString() + "-" + answer.ToString() + "-" + string.Format("{0:dd-MM-yyyy H:mm:ss tt}", DateTime.Now);
                    dr.SurveyProvider.UpdateSurvey(obj);
                    dr.VoteProvider.insert(vote);
                    flag = "1";
                    Response.Cookies["vote"].Value = vote.Ip;
                    Response.Cookies["vote"].Expires = DateTime.Now.AddDays(60);
                }

            }
            return Content(flag);
        }
    }
}
